<?php
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
class Send extends Model{
    public function msg($param){
		$ac = htmlspecialchars(urldecode(trim($param['ac'])));	
        $to = htmlspecialchars(urldecode(trim($param['to'])));		
		$type = htmlspecialchars(urldecode(trim($param['type'])));
		$data = $param['data'];
        if(!in_array($ac,['email','mobile']) || !in_array($type,['reg','bind','forgetpwd']) || empty($to )  || empty($type)){
			$this->error = '参数错误';
			return false;
        }
		$type_arr=['reg'=>'注册','bind'=>'绑定','forgetpwd'=>'找回密码'];
		$type_flag = $type_arr[$type];
		if($ac=='mobile'){
		    if(empty($to)){
			    $this->error = '手机号为空';
			    return false;					
			}
            if(!preg_match("/^1{1}\d{10}$/",$to)){
			        $this->error = '手机号格式不正确';
			        return false;
            }				
            if(empty($GLOBALS['config']['user']['sms']['sms_type'])){
			    $this->error = '未配置短信发送服务';
			    return false;				
            }
            $sms=model('sms');			
			$result = model('sms')->send_msg($type,$type_flag,$to);
			if($result>0){
				return $result;
			}else{
			    $this->error = $sms->getError();
			    return false;
			}	
		}else{
			if(empty($to)){
			    $this->error = '邮箱地址为空';
			    return false;					
			}
            if(!preg_match('/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/', $to)){
			    $this->error = '邮箱格式错误';
			    return false;
            }
            if(empty($GLOBALS['config']['user']['mail']['mail_type'])){
			    $this->error = '未配置邮件发送服务';
			    return false;				
            }			
			$email=new \app\common\library\Email();
			$result = $email->send_msg($type,$type_flag,$to,$data);
            if($result!== false){
                return $result;
            }else{
			    $this->error = $email->getError();
			    return false;					
            }			
		}
	}
	public function check_msg($param){
		$ac = htmlspecialchars(urldecode(trim($param['ac'])));			
		$type = htmlspecialchars(urldecode(trim($param['type'])));
		$code = htmlspecialchars(urldecode(trim($param['code'])));
		$to = htmlspecialchars(urldecode(trim($param['to'])));
		if(empty($code)){
			return false;
		}
		$cachename=md5(is_login().'_'.$to.'_'.$type);
		$cachecode=cache($cachename);
		if($cachecode==$code){
			cache($cachename,NULL);
			return true;
		}else{
			return false;
		}
	}	
}